package cn.itsource.hrm.controller;

import cn.itsource.hrm.doc.CourseDoc;
import cn.itsource.hrm.repository.CourseRepository;
import cn.itsource.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chenyuqi
 * @version v1.0.0
 * @date 2020/5/25
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseRepository courseRepository;

    /**
     * 保存
     * @param courseDoc
     * @return
     */
    @PostMapping("/save")
    public AjaxResult save(@RequestBody CourseDoc courseDoc){

        try {
            courseRepository.save(courseDoc);
            return AjaxResult.me().setSuccess(true).setMessage("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败"+e.getMessage());
        }
    }
    /**
     * 批量保存
     * @param courseDocs
     * @return
     */
    @PostMapping("/saveAll")
    public AjaxResult saveAll(@RequestBody List<CourseDoc> courseDocs){

        try {
            courseRepository.saveAll(courseDocs);
            return AjaxResult.me().setSuccess(true).setMessage("批量保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量保存失败"+e.getMessage());
        }
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/detele")
    public AjaxResult detele(@RequestParam("id") Long id){

        try {
            courseRepository.deleteById(id);
            return AjaxResult.me().setSuccess(true).setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }
    }
    /**
     * 删除
     * @param ids
     * @return
     */
    @PostMapping("/deteleAll")
    public AjaxResult deteleAll(@RequestBody List<Long> ids){

        try {
            for (Long id: ids) {
                courseRepository.deleteById(id);
            }
            return AjaxResult.me().setSuccess(true).setMessage("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败"+e.getMessage());
        }
    }
}
